import os
import xml.etree.ElementTree as et


project_list = []
default_cfgs = {}
tree = et.parse('default.xml')
root = tree.getroot()
for child in root:
    if child.tag == 'project':
        project_list.append(child.attrib)
    elif child.tag == 'default':
        default_cfgs = child.attrib

print("默认配置")
for cfg_type in default_cfgs.keys():
    print("%s:%s" % (cfg_type, default_cfgs[cfg_type]), end=' ')

print("\n开始拉取代码")
def get_target_branch(default_branch, project_branch):
    if project_branch is None:
        return default_branch
    else:
        return project_branch

for project in project_list:
    # 执行克隆
    clone_cmd = "git clone -b %s %s:%s %s" % (get_target_branch(default_cfgs['branch'], project.get('branch')), default_cfgs['url'], project['remote_path'], project['local_path'])
    print(clone_cmd)
    os.system(clone_cmd)
    print('done')

